package com.xcity.game.activity.newplayer.tracer;

import java.text.MessageFormat;

import com.xcity.db.entity.Player;
import com.xcity.db.entity.Relation2;
import com.xcity.game.activity.newplayer.NewPlayerDoingStr;
import com.xcity.game.activity.newplayer.NewPlayerTask;
import com.xcity.game.activity.newplayer.NewPlayerTracer;
import com.xcity.game.activity.newplayer.NewPlayerTracerLabel;
import com.xcity.game.relation.RelationService;

import naga.x.App;

@NewPlayerTracerLabel(NewPlayerTracer.FRIEND_COUNT)
public class FriendCountTracer implements NewPlayerTracer {

	@Override
	public boolean isDone(NewPlayerTask task) {
		int[] params = task.getTemplate().getParam();
		int[] progress = task.getProgress();
		if(progress[0] >= params[0]){
			return true;
		}
		return false;
	}

	@Override
	public void onChange(NewPlayerTask task, Object... vals) {
		int[] progress = task.getProgress();
		Player player = (Player)vals[0];
		int size = App.getApp().getServiceManager().get(RelationService.class).findRelations(player).sizeOf(Relation2.TYPE_FRIEND);
		progress[0] = size;
		if(progress[0] > PARAM0_MAX){
			progress[0] = PARAM0_MAX;
		}
	}

	@Override
	public String getDoingStr(NewPlayerTask task) {
		return MessageFormat.format(NewPlayerDoingStr.STR_FRIEND_COUNT, task.getTemplate().getParam()[0]);
	}

}
